﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;


namespace 药品信息管理
{
    public partial class YP_RZPZ_MX : Form
    {
        public YP_RZPZ_MX()
        {
            InitializeComponent();
        }

        public string pzid;
        db db1 = new db();
        private void YP_RZPZ_MX_Load(object sender, EventArgs e)
        {
            getSqlConnection myconnection = new 药品信息管理.getSqlConnection();
            SqlConnection conn2 = myconnection.GetCon();
            SqlCommand mycomm = new SqlCommand("select mc from YK_ZDGYS", conn2);
            SqlDataReader myread = mycomm.ExecuteReader();
            while (myread.Read())
            {
                this.comboBoxEx1.Items.Add((string)myread[0]);

            }
            conn2.Close();


            SqlConnection conn = new SqlConnection(SQL.conStr);
            DataTable dt1 = new DataTable();

            string str = @"SELECT GYS.MC 供应商,convert(datetime,FPRQ-2) 发票日期,FPH 发票号,RKDS 入库单数,FPJE 购入金额,LJJE 零价金额 FROM YK_FP0 FP
LEFT JOIN YK_ZDGYS GYS ON GYS.BM=FP.GYSBM WHERE FP.FLAG=0";
            SqlDataAdapter da = new SqlDataAdapter(str, conn);
            da.Fill(dt1);
            dataGridViewX2.DataSource = dt1;
            dataGridViewX2.Columns["供应商"].Visible = false;


        }

        private void dataGridViewX2_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {

            comboBoxEx1.Text = Convert.ToString(dataGridViewX2[0, dataGridViewX2.CurrentCell.RowIndex].Value).Trim();
            //dateTimePicker3.Text = Convert.ToString(dataGridViewX2[3, dataGridViewX2.CurrentCell.RowIndex].Value).Trim();
            textBoxX5.Text = Convert.ToString(dataGridViewX2[3, dataGridViewX2.CurrentCell.RowIndex].Value).Trim();
            textBoxX6.Text = Convert.ToString(dataGridViewX2[3, dataGridViewX2.CurrentCell.RowIndex].Value).Trim();
            textBoxX7.Text = Convert.ToString(dataGridViewX2[4, dataGridViewX2.CurrentCell.RowIndex].Value).Trim();
            textBoxX8.Text = Convert.ToString(dataGridViewX2[5, dataGridViewX2.CurrentCell.RowIndex].Value).Trim();
            dataGridViewX1.Rows.Clear();
            dataGridViewX1.Rows.Add(dataGridViewX2.CurrentRow.Cells[1].Value.ToString(),
                dataGridViewX2.CurrentRow.Cells[2].Value.ToString(),
                dataGridViewX2.CurrentRow.Cells[3].Value.ToString(),
                dataGridViewX2.CurrentRow.Cells[4].Value.ToString(),
                dataGridViewX2.CurrentRow.Cells[5].Value.ToString());


        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            string time1 = dateTimePicker1.Value.ToString("yyyy.MM.dd"), time2 = dateTimePicker2.Value.ToString("yyyy.MM.dd");
            SqlConnection conn = new SqlConnection(SQL.conStr);
            DataTable dt = new DataTable();
            string str = @"SELECT GYS.MC 供应商,convert(datetime,FPRQ-2) 发票日期,FPH 发票号,RKDS 入库单数,FPJE 购入金额,LJJE 零价金额 FROM YK_FP0 FP
LEFT JOIN YK_ZDGYS GYS ON GYS.BM=FP.GYSBM WHERE   FP.FPRQ >= " + db1.datetoint(time1) + " AND FP.FPRQ <= " + db1.datetoint(time2);
            SqlDataAdapter da = new SqlDataAdapter(str, conn);
            da.Fill(dt);
            dataGridViewX2.DataSource = dt;
            dataGridViewX2.Columns["供应商"].Visible = false;
        }

        private void buttonX2_Click(object sender, EventArgs e)
        {
            dataGridViewX1.Rows.Clear();
            dataGridViewX1.Rows.Add(dataGridViewX2.CurrentRow.Cells[1].Value.ToString(),
                dataGridViewX2.CurrentRow.Cells[2].Value.ToString(),
                dataGridViewX2.CurrentRow.Cells[3].Value.ToString(),
                dataGridViewX2.CurrentRow.Cells[4].Value.ToString(),
                dataGridViewX2.CurrentRow.Cells[5].Value.ToString());
        }

        private void buttonItem1_Click(object sender, EventArgs e)
        {
            string sqlSelect = "select max(PZID) from YK_RZPZ";
            int PZID = 0;
            string FPH = dataGridViewX1.Rows[0].Cells[1].Value.ToString();
            string time = dateTimePicker3.Value.ToString("yyyy.MM.dd");
            string ValidatePZH = null;
            DataTable DtSelect = db1.sqldt(sqlSelect);
            DataTable DtYK_RZPZ = new DataTable();
            if (!string.IsNullOrEmpty(DtSelect.Rows[0][0].ToString()))
            {
                PZID = Convert.ToInt32(DtSelect.Rows[0][0].ToString()) + 1;
            }
            string sqlYK_RZPZ = "insert into YK_RZPZ (PZID,FPZS,RQ,RKDS,PZH,JJJE,GYSBM,LJJE,BZ) values ('" + PZID + "','" + textBoxX5.Text.Trim() + "','" + db1.datetoint(time) + "','" + textBoxX6.Text.Trim() + "','" + textBoxX3.Text.Trim() + "','" + textBoxX7.Text.Trim() + "','" + comboBoxEx1.Text.ToString() + "','" + textBoxX8.Text.Trim() + "','" + textBoxX4.Text.Trim() + "')";
            string sqlUpdate = "update YK_FP0 set PZID = '" + PZID + "' where FPH = '" + FPH + "'";
            string sqlValidatePZH = "select * from YK_RZPZ where PZH = '" + textBoxX3.Text.Trim() + "'";
            ValidatePZH = db1.sqlstr(sqlValidatePZH);
            if (string.IsNullOrEmpty(ValidatePZH))
            {
                if (db1.Excutesql(sqlYK_RZPZ) && db1.Excutesql(sqlUpdate))
                {
                    MessageBox.Show("保存成功 ");
                }
                else
                {
                    MessageBox.Show("555 , 错误");
                }
            }
            else
            {
                MessageBox.Show("请不要插入相同的记录");
            }

        }
    }
}